﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Common;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Application.SystemManager
{
    public class WorksheetApp
    {
        private readonly WorksheetRepository _service = new WorksheetRepository();

        public IEnumerable<ProductTechnologyView> GetList(int status, string productName, string productNum, DateTime beginTime, DateTime endTime)
        {
            return _service.GetList(status, productName, productNum, beginTime, endTime).OrderByDescending(a => a.Status).ThenByDescending(a => a.ApplyTime);
            //return _service.IQueryable().Where(a => a.Status == status || status == -10).AsQueryable().OrderByDescending(a => a.CreateTime);
        }
        public IEnumerable<ProductTechnologyView> GetListSortByType(int status, string productName, string productNum, DateTime beginTime, DateTime endTime)
        {
            var num = "2,1,3".Split(',').Select(v => Convert.ToInt32(v)).ToArray();
            //return _service.GetList(status, productName, productNum, beginTime, endTime).ToList().OrderBy(a=> Array.IndexOf(num.Reverse().ToArray(), a.Type)).ThenByDescending(a=>a.ApplyTime);
            return _service.GetListWhere(status, productName, productNum, beginTime, endTime).ToList().OrderBy(a => Array.IndexOf(num, a.Status)).ThenBy(a => a.WorksheetStatus).ThenByDescending(a => a.ApplyTime);
        }

        public IEnumerable<Worksheet> GetSubList(Guid id)
        {
            return _service.IQueryable(a => a.ParentId != null && a.ParentId == id && a.Type != 2);
        }
        public IEnumerable<Worksheet> GetSubListTotal(Guid id)
        {
            return _service.IQueryable(a => a.ParentId != null && a.ParentId == id);
        }

        public Worksheet GetSingleWorksheet(Guid id)
        {
            return _service.FindEntity(a => a.Id == id);
        }

        public int Update(Worksheet ws)
        {
            return _service.Update(ws);
        }

        public int Add(Worksheet ws)
        {
            return _service.Insert(ws);
        }

        public int Delete(Guid id)
        {
            return _service.Delete(a => a.Id == id);
        }
        public int DeleteByParentId(Guid id)
        {
            return _service.Delete(a => a.ParentId == id);
        }
    }
}
